package com.laoxiang.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoxiang.execption.LxAdminException;
import com.laoxiang.modules.system.entity.SysPost;
import com.laoxiang.modules.system.entity.SysUserPost;
import com.laoxiang.modules.system.entity.dto.PageDTO;
import com.laoxiang.modules.system.entity.vo.QueryPostVO;
import com.laoxiang.modules.system.mapper.SysPostMapper;
import com.laoxiang.modules.system.service.SysPostService;
import com.laoxiang.utils.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 岗位信息表(SysPost)表服务实现类
 *
 * @author db
 * @since 2023-09-03 22:43:56
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    @Override
    public List<SysPost> getPostList() {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPost::getStatus,1);
        List<SysPost> sysPostList = sysPostMapper.selectList(queryWrapper);
        return sysPostList;
    }

    @Override
    public PageDTO getPosts(Integer pageNum, Integer pageSize, QueryPostVO queryPostVO) {
        // 构建查询条件
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(queryPostVO.getPostName()),SysPost::getName,queryPostVO.getPostName());
        queryWrapper.eq(Objects.nonNull(queryPostVO.getStatus()),SysPost::getStatus,queryPostVO.getStatus());
        queryWrapper.orderByAsc(SysPost::getCreateTime);

        if(Objects.isNull(pageNum)){
            pageNum = 1;
        }
        if(Objects.isNull(pageSize)){
            pageSize = 10;
        }
        Page<SysPost> sysPostPage = sysPostMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        // 封装pageDTO
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageIndex(pageNum);
        pageDTO.setPageSize(pageSize);
        pageDTO.setTotal(sysPostPage.getTotal());
        pageDTO.setRows(sysPostPage.getRecords());
        return pageDTO;
    }

    @Override
    public void addPost(SysPost sysPost) {
        // 检查岗位名称是否唯一
        boolean b = checkPostNameUnique(sysPost.getName());
        if(b){
            throw new LxAdminException(AppHttpCodeEnum.POST_EXIST);
        }

        sysPostMapper.insert(sysPost);
    }

    @Override
    public SysPost getPostById(Long postId) {
        SysPost sysPost = sysPostMapper.selectById(postId);
        return sysPost;
    }

    @Override
    public void updatePost(SysPost sysPost) {
        // 检查岗位名称是否唯一
        boolean b = checkPostNameUnique(sysPost.getName());
        if(!b){
            throw new LxAdminException(AppHttpCodeEnum.POST_EXIST);
        }

        sysPostMapper.updateById(sysPost);
    }

    @Override
    public void deletePostById(Long postId) {
        sysPostMapper.deleteById(postId);
    }

    @Override
    public boolean checkPostNameUnique(String postName) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(postName),SysPost::getName,postName);
        Integer count = sysPostMapper.selectCount(queryWrapper);
        return count>0;
    }

    @Override
    public void batchDeletePost(List<Long> postIds) {
        sysPostMapper.deleteBatchIds(postIds);
    }
}

